<?php

declare (strict_types = 1);

namespace app\common\service\user;

use app\common\service\BaseService;
use app\common\repository\user\WithdrawalApplyRepository;

class WithdrawalApplyService extends BaseService
{
    public function __construct(WithdrawalApplyRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取列表
     *
     * @param array $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     */
    public function getList(array $data = []): array
    {
        [$page, $pageSize] = $this->getPaginationParams();
        $where = [];
        return $this->repository->paginate($where, ['create_time' => 'desc'], $page, $pageSize);
    }

    /**
     * 新增
     *
     * @param array $data
     * @return \think\Collection|\app\common\model\BaseModel|array
     * @throws \Exception
     */
    public function create(array $data): \think\Collection|\app\common\model\BaseModel|array
    {
        $data['apply_time'] = time(); // 申请时间
        $data['order_no'] = get_id(); // 订单号
        return $this->repository->save($data);
    }

    /**
     * 修改
     *
     * @param array $data
     * @param $where
     * @return \app\common\model\BaseModel|bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update(array $data, $where): \app\common\model\BaseModel|bool|int
    {
        return $this->repository->update($data, $where);
    }

    /**
     * 获取详情
     *
     * @param $where
     * @return \app\common\model\BaseModel|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfo($where): ?\app\common\model\BaseModel
    {
        return $this->repository->getOne($where);
    }

    /**
     * 删除
     *
     * @param $where
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete($where): bool
    {
        return $this->repository->delete([
            'id' => $where['id']
        ]);
    }
}